#include "StdAfx.h"
#include "WorkModeCon.h"
#include "Util/UtilInclude.h"
#include "WorkOut.h"



#ifndef UNDER_CE
#undef	RETAILMSG
#define RETAILMSG
#endif



CWorkModeCon::CWorkModeCon(HWND hWnd)
//:CImproveCon( hWnd )
:CContainer(hWnd)	
{

}

CWorkModeCon::~CWorkModeCon(void)
{
}

BOOL CWorkModeCon::InitControls(DWORD dwData)
{
	LANGUAGE_DECLARE;
	FONT_DECLARE;
	if(!m_config.Init(L"WorkMode.xml"))
	{
		g_App.ShowMsgBox(CMD_MSGBOX_OK,L"",LAN_GETTEXT(Msg_ResError));
		return FALSE;
	}

	BOOL bFromDll	= m_config.IsResFromDll();
	wstring str		= m_config.GetImage();	

	wstring strResPath = TEXT("WorkMode\\");

	HBITMAP hBitmap = CUtil::LoadBitmapEx( strResPath+str,m_config.m_bFromDll);
	SetBackGnd(hBitmap);
	SetRect( m_config.GetRect() );

	//int nFunction = CConfig::GetInstance()->GetFunction();
	//wstring strBtn = CConfig::GetInstance()->GetMainPage();


	BtnParam btnInfo;
	TextParam txtInfo;
	BatTimePicParam  bat_time_pic;
	IBmpNumInfo      INumInfo;

	CText*		pTxt = NULL;


	if( m_config.GetBtnParam(btnInfo,L"Btn_Manual")&& !btnInfo.m_rc.IsRectEmpty() )
	{
		m_pBtnManual = (CBmpBtn*)this->AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_WORKMODE_MANUAL));
		m_config.SetBtnParam(m_pBtnManual,btnInfo);	
	}

	if( m_config.GetBtnParam(btnInfo,L"Btn_Pyramid")&& !btnInfo.m_rc.IsRectEmpty() )
	{
		m_pBtnPyramid = (CBmpBtn*)this->AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_WORKMODE_PYRAMID));
		m_config.SetBtnParam(m_pBtnPyramid,btnInfo);	
	}

	if( m_config.GetBtnParam(btnInfo,L"Btn_HillClimb")&& !btnInfo.m_rc.IsRectEmpty() )
	{
		m_pBtnHillClimb = (CBmpBtn*)this->AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_WORKMODE_HILLCLIMB));
		m_config.SetBtnParam(m_pBtnHillClimb,btnInfo);	
	}

	if( m_config.GetBtnParam(btnInfo,L"Btn_Interval")&& !btnInfo.m_rc.IsRectEmpty() )
	{
		m_pBtnInterval = (CBmpBtn*)this->AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_WORKMODE_INTERVAL));
		m_config.SetBtnParam(m_pBtnInterval,btnInfo);	
	}

	if( m_config.GetBtnParam(btnInfo,L"Btn_Random")&& !btnInfo.m_rc.IsRectEmpty() )
	{
		m_pBtnRandom = (CBmpBtn*)this->AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_WORKMODE_RANDOM));
		m_config.SetBtnParam(m_pBtnRandom,btnInfo);	
	}

	if( m_config.GetBtnParam(btnInfo,L"Btn_CstWatts")&& !btnInfo.m_rc.IsRectEmpty() )
	{
		m_pBtnCstWatts = (CBmpBtn*)this->AddControl(new CBmpBtn(SBS_RECT,L"",btnInfo.m_rc, CMD_WORKMODE_CONSTANTWATTS));
		m_config.SetBtnParam(m_pBtnCstWatts,btnInfo);	
	}

	if( m_config.GetBtnParam(btnInfo,L"Btn_Cancel")&& !btnInfo.m_rc.IsRectEmpty() )
	{
		CBmpBtn* pBtn = (CBmpBtn*)this->AddControl(new CBmpBtn(SBS_RECT,LAN_GETTEXT(MSG_CANCEL),btnInfo.m_rc, CMD_WORKMODE_CANCEL));
		m_config.SetBtnParam(pBtn,btnInfo);	
	}


	CControl* pCtrl = NULL;
	CRect rcTemp;
	for ( int i=0;i<m_vecControls.size();i++ )
	{
		pCtrl = m_vecControls[i];
		if ( NULL!=pCtrl )
		{
			rcTemp = pCtrl->GetRect();
			rcTemp.OffsetRect( -m_rcRect.left,-m_rcRect.top );
			pCtrl->SetRect( rcTemp );
		}
	}



	return TRUE;
}

void CWorkModeCon::OnCommand(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{

	int nNum;
}

BOOL CWorkModeCon::AlphaBlendMaskBk( CDC *pDCBK, HBITMAP hbmpBK,int nalpha )
{
	CDC dcBK;
	dcBK.CreateCompatibleDC( pDCBK->m_hDC );
	HBITMAP hBmpBKOld = dcBK.SelectBitmap( hbmpBK );
	BLENDFUNCTION blender;
	blender.BlendOp = AC_SRC_OVER;
	blender.BlendFlags = 0;
	blender.SourceConstantAlpha = nalpha;
	blender.AlphaFormat   =  0;

	BOOL bRet = ::AlphaBlend( pDCBK->m_hDC,m_rcRect.left,m_rcRect.top,m_rcRect.Width(),m_rcRect.Height(),
		dcBK.m_hDC,0,0,m_rcRect.Width(),m_rcRect.Height(),blender);

	if ( NULL!=hBmpBKOld )
		dcBK.SelectBitmap( hBmpBKOld );
	dcBK.DeleteDC();
	return TRUE;
}

void CWorkModeCon::ShowAnimate(  CDC* pDCFront,  CDC* pDCBK )
{
	DWORD dwStartT,dwEndT;
	dwStartT = GetTickCount();
	DWORD dwStartTALL = GetTickCount();

	HBITMAP hbmpBK,hBmpBKOld;
	hbmpBK = GetBackGnd();
	if (NULL==hbmpBK)
		return ;

	CDC memdc;
	memdc.CreateCompatibleDC( pDCFront->m_hDC );
	HBITMAP	hbmpMem = CreateCompatibleBitmap( pDCFront->m_hDC,m_rcRect.Width(),m_rcRect.Height() );
	HBITMAP hbmpOldMem = memdc.SelectBitmap( hbmpMem );
	memdc.BitBlt( m_rcRect.left,m_rcRect.top,m_rcRect.Width(),m_rcRect.Height(),pDCBK->m_hDC,m_rcRect.left,m_rcRect.top,SRCCOPY );


	int nalpha = 120;
	AlphaBlendMaskBk( &memdc, GetBackGnd(),nalpha );


	dwEndT = GetTickCount();
	RETAILMSG(1,(L"#######  %d r\n",dwEndT-dwStartT));
	dwStartT = GetTickCount();


	CDC dcOneCtrl;
	dcOneCtrl.CreateCompatibleDC( pDCFront->m_hDC );
	CBmpBtn* m_pBtn[6] = { m_pBtnManual,m_pBtnPyramid,m_pBtnHillClimb,m_pBtnInterval,m_pBtnRandom,m_pBtnCstWatts };
	m_nAniFrame = 6;
	for ( int i=0;i<m_nAniFrame;i++ )
	{
		memdc.BitBlt( 0,0,m_rcRect.Width(),m_rcRect.Height(),pDCBK->m_hDC,0,0,SRCCOPY );

		BLENDFUNCTION blender;

		HBITMAP hBmpIcoOld;
		//hBmpIcoOld = dcOneCtrl.SelectBitmap( m_pBtn[i]->GetPic() );
		blender.BlendOp = AC_SRC_OVER;
		blender.BlendFlags = 0;
		blender.SourceConstantAlpha = 255;
		blender.AlphaFormat   =  AC_SRC_ALPHA;

		CRect rcIco = m_pBtn[i]->GetRect();
		BOOL bRet = ::AlphaBlend( memdc.m_hDC,rcIco.left,rcIco.top,rcIco.Width(),rcIco.Height(),dcOneCtrl.m_hDC,
			0,0,rcIco.Width(),rcIco.Height(),blender);
		
		pDCFront->BitBlt( 0,0,m_rcRect.Width(),m_rcRect.Height(),memdc.m_hDC,0,0,SRCCOPY );
		dcOneCtrl.SelectBitmap( hBmpIcoOld );
		dwEndT = GetTickCount();
		RETAILMSG(1,(L"####### i= %d r\n",dwEndT-dwStartT));
		dwStartT = GetTickCount();
	}

	dcOneCtrl.DeleteDC();


	memdc.SelectBitmap( hbmpOldMem );
	DeleteObject( hbmpMem );
	memdc.DeleteDC();

	DWORD dwEndTALL = GetTickCount();
	RETAILMSG( 1,(L"#######  all=%d r\n",dwEndTALL-dwStartTALL) );

}

void  CWorkModeCon::Paint(CDC *pDC)
{
	CDC memdc;
	memdc.CreateCompatibleDC( NULL );
	HBITMAP hBmpBK = GetBackGnd();
	HBITMAP hbmpOldMem = memdc.SelectBitmap( hBmpBK );

	CControl* pCtrl;
	for ( int i=0;i<m_vecControls.size();i++ )
	{
		pCtrl = m_vecControls[i];
		if ( NULL!=pCtrl )
		{
			pCtrl->Paint( &memdc );
		}
	}

	int nalpha = 245;
	BLENDFUNCTION blender;
	blender.BlendOp = AC_SRC_OVER;
	blender.BlendFlags = 0;
	blender.SourceConstantAlpha = nalpha;
	blender.AlphaFormat   =  0;

	BOOL bRet = ::AlphaBlend( pDC->m_hDC,m_rcRect.left,m_rcRect.top,m_rcRect.Width(),m_rcRect.Height(),
		memdc.m_hDC,0,0,m_rcRect.Width(),m_rcRect.Height(),blender);

	memdc.SelectBitmap( hbmpOldMem );
	memdc.DeleteDC();

}

void  CWorkModeCon::OnLButtonDown(UINT nFlags, CPoint point)
{
	CPoint ptCon = point -m_rcRect.TopLeft() ;
	
	if ( NULL!=m_pHotControl )
	{
		m_pHotControl->SetState( STATE_NORMAL );
		InvalidateRect( m_hWnd,m_pHotControl->GetRect(),FALSE );
		m_pHotControl = NULL;
	}

	CControl* pCtrl = NULL;
	CRect rcTemp;
	for ( int i=0;i<m_vecControls.size();i++ )
	{
		pCtrl = m_vecControls[i];
		if ( NULL!=pCtrl && pCtrl->IsVisible() && pCtrl->IsEnabled() &&  pCtrl->GetRect().PtInRect(ptCon) )
		{
			m_pHotControl = pCtrl;
			m_pHotControl->SetState( STATE_PUSHED );
			rcTemp = m_pHotControl->GetRect();
			rcTemp.OffsetRect( m_rcRect.TopLeft() );
			InvalidateRect( m_hWnd,rcTemp,FALSE );
			UpdateWindow( m_hWnd );
		}
	}
}

void  CWorkModeCon::OnLButtonUp(UINT nFlags, CPoint point)
{
	if ( NULL != m_pHotControl )
	{
		m_pHotControl->SetState( STATE_NORMAL );

		CRect rcTemp = m_pHotControl->GetRect();
		rcTemp.OffsetRect( m_rcRect.TopLeft() );

		InvalidateRect( m_hWnd,rcTemp,FALSE );
		UpdateWindow( m_hWnd );

		if( rcTemp.PtInRect( point ) )
			PostMessage( m_hWnd,WM_COMMAND, m_pHotControl->GetID(),NULL);
	}
	m_pHotControl = NULL;
}
